package com.lyq.controller;

import com.lyq.mapper.DepartmentMapper;
import com.lyq.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyq
 * @since 2020-05-19
 */
@Controller
@RequestMapping("/department")
public class DepartmentContentController {
    @Autowired
    private DepartmentMapper departmentMapper;

    @RequestMapping("/departmentList")
    public String departmentList(Model model){
        List<Department> departments = departmentMapper.selectList(null);
        model.addAttribute("departmentList",departments);
        return "department/departmentList";
    }
    @GetMapping("/toAddDepartment")
    public String toAddDepartment(){
        return "department/addDepartment.html";
    }
    @PostMapping("/addDepartment")
    public String addDepartment(Department department){
        int insert = departmentMapper.insert(department);
        System.out.println(insert);
        return "redirect:/department/departmentList";
    }
    @GetMapping("/toUpdateDepartment/{id}")
    public String toUpdateDepartment(@PathVariable("id")Integer id, Model Model){
        Department department = departmentMapper.selectById(id);
        Model.addAttribute("department",department);
        return "department/updateDepartment.html";
    }
    @PostMapping("/updateDepartment")
    public String updateDepartment(Department department){
        int byId = departmentMapper.updateById(department);
        System.out.println(byId);
        return "redirect:/department/departmentList";
    }
    @GetMapping("/deleteDepartment/{id}")
    public String deleteDepartment(@PathVariable("id")Integer id){
        int i = departmentMapper.deleteById(id);
        return "redirect:/department/departmentList";
    }


}

